using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Windows.Media;
using Styx;
using Styx.Helpers;
using Styx.Common;
using Styx.CommonBot.POI;
using Styx.CommonBot;
using Styx.CommonBot.Routines;
using Styx.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;


namespace ElementalMyDear
{
    public partial class Classname : CombatRoutine
    {
        public override sealed string Name { get { return "Elemental Shaman by Pasterke"; } }

        public override WoWClass Class { get { return WoWClass.Shaman; } }
         
        private static LocalPlayer Me { get { return StyxWoW.Me; } }
 
        public override bool WantButton{ get { return true; } }

        public override void OnButtonPress()
        {
            ElementalMyDear.Form1 ConfigForm = new Form1();
            ConfigForm.ShowDialog();
        }
        public override void Initialize()
        {
            Updater.CheckForUpdate();
            Lua.Events.AttachEvent("MODIFIER_STATE_CHANGED", HandleModifierStateChanged);
        }

        public bool RemoveDeadTargets = true;

        public static Stopwatch fightTimer = new Stopwatch();

        public Stopwatch flametongueTimer = new Stopwatch();

        public Stopwatch OutOfCombatTimer = new Stopwatch();


        //fire totems
        public Stopwatch SearingTimer = new Stopwatch();

        public Stopwatch MagmaTimer = new Stopwatch();

        public Stopwatch FireElementalTimer = new Stopwatch();


        // earth totems
        public Stopwatch EarthElementalTimer = new Stopwatch();



        public Stopwatch MoveBackTimer = new Stopwatch();

        public bool PutTotems = false;

        public bool CastEarthQuake = false;

        public bool AscendanceOn = false;

        public bool useEartElementalTotem = false;

        public override void Pulse()
        {
            try
            {
                if (fightTimer.IsRunning
                    && fightTimer.Elapsed.Seconds >= 30)
                {
                    Blacklist.Add(Me.CurrentTarget, BlacklistFlags.All, new TimeSpan(0, 3, 0));
                    fightTimer.Reset();
                    LogMsg("Take too much time to attack, blacklisting current target", 1);
                    return;
                }
                if (Me.Combat && Me.CurrentTarget.IsFriendly)
                {
                    Me.ClearTarget();
                    LogMsg("Friendly Target removed because I'm in Combat (" + Me.CurrentTarget.Name + ")", 1);
                    return;
                }
                if (!Me.CurrentTarget.IsPlayer && Me.CurrentTarget.IsFriendly)
                {
                    return;
                }
                if (Me.Combat && RemoveDeadTargets && Me.CurrentTarget.IsDead)
                {
                    Me.ClearTarget();
                    LogMsg("Dead Target removed (" + Me.CurrentTarget.Name + ")", 1);
                    return;
                }
                if (SearingTimer.IsRunning
                    && SearingTimer.Elapsed.Seconds > 58)
                {
                    SearingTimer.Reset();
                }
                if (MagmaTimer.IsRunning
                    && MagmaTimer.Elapsed.Seconds > 58)
                {
                    MagmaTimer.Reset();
                }
                if (FireElementalTimer.IsRunning
                    && FireElementalTimer.Elapsed.Seconds > 58)
                {
                    FireElementalTimer.Reset();
                    useEartElementalTotem = true;
                }
                 
            }
            catch { }
        }

        #region CC_Begin


        public override bool NeedRest
        {
            get
            {
                if (UseSpells)
                {
                    if (Me.ManaPercent <= ShammySettings.myPrefs.drinkpercent)
                    {
                        return true;
                    }
                    if (Me.HealthPercent <= 75)
                    {
                        return true;
                    }
                }
                return false;
            }
        }

        public override void Rest()
        {
            if (UseSpells)
            {
                if (Me.ManaPercent <= ShammySettings.myPrefs.drinkpercent)
                {
                    Styx.CommonBot.Rest.Feed();
                }
                if (Me.HealthPercent <= ShammySettings.myPrefs.healingsurgeOOCpercent
                    && Me.HealthPercent > ShammySettings.myPrefs.eatpercent)
                {
                    if (SpellManager.HasSpell("Healing Surge")
                        && !Me.IsCasting)
                    {
                        SpellManager.Cast("Healing Surge");
                        LogMsg("Healing Surge", 2);
                    }
                }
                if (Me.HealthPercent <= ShammySettings.myPrefs.eatpercent)
                {
                    Styx.CommonBot.Rest.Feed();
                }
            }
        }

        #endregion

        #region Pull

        public override void Pull()
        {

            MoveInRange(35f);
            Me.CurrentTarget.Face();

            if (gotTarget
                && !Blacklist.Contains(Me.CurrentTarget, BlacklistFlags.All))
            {
                fightTimer.Start();
            }
            if (gotTarget
                && !spellOnCooldown(FlameShock))
            { 
                if (SpellManager.CanCast(FlameShock))
                {
                    SpellManager.Cast(FlameShock);
                    LogMsg("Flame Shock", 6);
                    fightTimer.Reset();
                }
            }
            if (gotTarget
                && SpellManager.HasSpell(LightningBolt)
                && !spellOnCooldown(LightningBolt))
            {
                if (SpellManager.CanCast(LightningBolt))
                {
                    SpellManager.Cast(LightningBolt);
                    LogMsg("Lightning Bolt", 6);
                    fightTimer.Reset();
                }
            }
        }


        #endregion

        #region Pull Buffs

        public override bool NeedPullBuffs { get { return false; } }

        public override void PullBuff() { }

        #endregion

        #region Pre Combat Buffs

        public override bool NeedPreCombatBuffs 
        { 
            get 
            {
                if (UseSpells)
                {  
                    if (PutTotems)
                    {
                        OutOfCombatTimer.Start();
                        if (OutOfCombatTimer.Elapsed.Seconds >= 2)
                        {
                            ResetTotems();
                            if(SpellManager.HasSpell(TotemicRecall))
                            {
                                SpellManager.Cast(TotemicRecall);
                            }
                            PutTotems = false;
                            CastEarthQuake = false;
                            useEartElementalTotem = false;
                            OutOfCombatTimer.Reset();
                        }
                    }
                    //Flametongue Weapon
                    if ((!flametongueTimer.IsRunning || flametongueTimer.Elapsed.TotalMinutes > 58)
                        && SpellManager.HasSpell(FlametongueWeapon)
                        && LastSpellID != FlametongueWeapon)
                    {
                        if (SpellManager.CanCast(FlametongueWeapon))
                        {
                            flametongueTimer.Restart();
                            SpellManager.Cast(FlametongueWeapon);
                            LogMsg("Flametongue Weapon", 2);
                            LastSpellID = FlametongueWeapon;
                        }
                    }

                    //Lightning Shield
                    if (!buffExists(LightningShield, Me)
                        && SpellManager.HasSpell(LightningShield)
                        && LastSpellID != LightningShield)
                    {
                        if (SpellManager.CanCast(LightningShield))
                        {
                            SpellManager.Cast(LightningShield);
                            LogMsg("Lightning Shield", 2);
                            LastSpellID = LightningShield;
                        }
                    }

                    AlchemistFlask();
                }
                return false; 
            } 
        }

        public override void PreCombatBuff()
        {
            return;
        }

        #endregion

        #region Combat Buffs

        public override bool NeedCombatBuffs { get { return false; } }
             

        public override void CombatBuff()
        {

        }

        #endregion

        #region Heal

        public override bool NeedHeal 
        { 
            get 
            { 
                return false; 
            } 
        }

        public override void Heal()
        {

        }

        #endregion

        #region Falling

        public void HandleFalling() { }

        #endregion

        #region Combat

        public override void Combat()
        {
            if (Paused)
            {
                return;
            }
            if (UseSpells)
            {
                if(autoAttacking())
                {
                    autoAttack();
                }
                //Flametongue Weapon
                if ((!flametongueTimer.IsRunning || flametongueTimer.Elapsed.TotalMinutes > 58)
                    && SpellManager.HasSpell(FlametongueWeapon)
                    && LastSpellID != FlametongueWeapon)
                {
                    if (SpellManager.CanCast(FlametongueWeapon))
                    {
                        flametongueTimer.Restart();
                        SpellManager.Cast(FlametongueWeapon);
                        LogMsg("Flametongue Weapon", 2);
                        LastSpellID = FlametongueWeapon;
                    }
                }

                //Lightning Shield
                if (!Me.HasAura(LightningShield)
                    && SpellManager.HasSpell(LightningShield)
                    && LastSpellID != LightningShield)
                {
                    if (SpellManager.CanCast(LightningShield))
                    {
                        SpellManager.Cast(LightningShield);
                        LogMsg("Lightning Shield", 2);
                        LastSpellID = LightningShield;
                    }
                }
                if (SpellManager.HasSpell(HealingSurge)
                    && Me.HealthPercent <= ShammySettings.myPrefs.healingsurgepercent
                    && !Me.IsCasting)
                {
                    if (SpellManager.CanCast(HealingSurge))
                    {
                        SpellManager.Cast(HealingSurge);
                        LogMsg("Healing Surge", 4);
                    }
                }
                if (ShammySettings.myPrefs.healthstone == 1
                    && Me.HealthPercent <= ShammySettings.myPrefs.healthstonepercent)
                {
                    HealthStone();
                }
                if (ShammySettings.myPrefs.healthpotion == 1
                    && Me.HealthPercent <= ShammySettings.myPrefs.healthpotionpercent)
                {
                    myHealthPotion();
                }
                /*if (ShammySettings.myPrefs.naruu == 1
                    && Me.HealthPercent <= ShammySettings.myPrefs.naruupercent
                    && !spellOnCooldown("Gift of the Naruu"))
                {
                    if (SpellManager.CanCast("Gift of the Naruu"))
                    {
                        SpellManager.Cast("Gift of the Naruu");
                        LogMsg("Gift of the Naruu", 2);
                    }
                }*/
                /*if (ShammySettings.myPrefs.LifeSpirit == 1
                    && Me.HealthPercent <= ShammySettings.myPrefs.LifeSpiritpercent
                    && !spellOnCooldown("Life Spirit"))
                {
                    LifeSpirit();
                }*/
                if (ShammySettings.myPrefs.Mengibelt == 1
                    && Me.HealthPercent <= ShammySettings.myPrefs.engibeltpercent)
                {
                    UseEngibelt();
                }
                if (gotTarget
                    && !ShammySettings.myPrefs.DisableFacing)
                {
                    Me.CurrentTarget.Face();
                }
                if (gotTarget
                    && !ShammySettings.myPrefs.DisableMovement)
                {
                    MoveInRange(35);
                }

                AlchemistFlask();
                myPotion();

                 
                if (gotTarget
                    && addCount >= ShammySettings.myPrefs.startaoe)
                {
                    AoeRotation();
                }
                if (gotTarget
                    && addCount < ShammySettings.myPrefs.startaoe)
                {
                    MainRotation();
                }
            }

        }

        #endregion
       
        #region rotation
        public void MainRotation()
        {
            if (UseSpells)
            {
                if (lightningbolt() && Me.IsMoving)
                {
                    if (SpellManager.CanCast(LightningBolt))
                    {
                        SpellManager.Cast(LightningBolt);
                        LogMsg("Lightning Bolt", 6);
                        LastSpellID = LightningBolt;
                        return;
                    }
                }
                if (AscendanceOn
                    && !Me.HasAura("Ascendance")
                    && !spellOnCooldown(Ascendance))
                {
                    AscendanceOn = false;
                }
                if (flameschok())
                {
                    if (SpellManager.CanCast(FlameShock))
                    {
                        SpellManager.Cast(FlameShock);
                        LogMsg("Flame Shock", 1);
                        LastSpellID = FlameShock;
                        return;
                    }
                }
                if (unleashelements())
                {
                    if (SpellManager.CanCast(UnleashElements))
                    {
                        SpellManager.Cast(UnleashElements);
                        LogMsg("Unleash Elements", 3);
                        LastSpellID = UnleashElements;
                        return;
                    }
                }
                if (shamanisticrage())
                {
                    if (SpellManager.CanCast(ShamanisticRage))
                    {
                        SpellManager.Cast(ShamanisticRage);
                        LogMsg("Shamanistic Rage", 3);
                        LastSpellID = ShamanisticRage;
                        return;
                    }
                }
                if (lavaburstAscendance())
                {
                    if (SpellManager.CanCast(LavaBurst))
                    {
                        SpellManager.Cast(LavaBurst);
                        LogMsg("Lava Burst", 4);
                        LastSpellID = LavaBurst;
                        return;
                    }
                }
                if (earthshock())
                {
                    if (SpellManager.CanCast(EarthShock))
                    {
                        SpellManager.Cast(EarthShock);
                        LogMsg("Earth Shock", 0);
                        LastSpellID = EarthShock;
                        return;
                    }
                }
                if (bloodfury())
                {
                    if (SpellManager.CanCast(BloodFury))
                    {
                        SpellManager.Cast(BloodFury);
                        LogMsg("Blood Fury", 1);
                        LastSpellID = BloodFury;
                    }
                }
                if (berserking())
                {
                    if (SpellManager.CanCast(Berserking))
                    {
                        SpellManager.Cast(Berserking);
                        LogMsg("Berserking", 1);
                        LastSpellID = Berserking;
                    }
                }

                UseMyTrinkets();

                if (ascendance())
                {
                    if (SpellManager.CanCast(Ascendance))
                    {
                        SpellManager.Cast(Ascendance);
                        LogMsg("Ascendance", 2);
                        LastSpellID = Ascendance;
                        AscendanceOn = true;
                        return;
                    }
                }
                if (fireElementalTotem())
                {
                    if (SpellManager.CanCast(FireElementalTotem))
                    {
                        SpellManager.Cast(FireElementalTotem);
                        LogMsg("Fire Elemental Totem", 2);
                        LastSpellID = FireElementalTotem;
                        FireElementalTimer.Restart();
                        useEartElementalTotem = false;
                        PutTotems = true;
                        return;
                    }
                }
                if (earthElementalTotem())
                {
                    if (SpellManager.CanCast(EarthElementalTotem))
                    {
                        SpellManager.Cast(EarthElementalTotem);
                        LogMsg("Earth Elemental Totem", 2);
                        LastSpellID = EarthElementalTotem;
                        EarthElementalTimer.Restart();
                        PutTotems = true;
                        return;
                    }
                }
                if (searingTotem())
                {
                    if (SpellManager.CanCast(SearingTotem))
                    {
                        SpellManager.Cast(SearingTotem);
                        LogMsg("Searing Totem", 2);
                        LastSpellID = SearingTotem;
                        SearingTimer.Restart();
                        PutTotems = true;
                        return;
                    }
                }
                if (elementalblast())
                {
                    if (SpellManager.CanCast(ElementalBlast))
                    {
                        SpellManager.Cast(ElementalBlast);
                        LogMsg("Elemental Blast", 6);
                        LastSpellID = ElementalBlast;
                        return;
                    }
                }
                if (lifeblood())
                {
                    if (SpellManager.CanCast(Lifeblood))
                    {
                        SpellManager.Cast(Lifeblood);
                        LogMsg("Lifeblood", 9);
                        LastSpellID = Lifeblood;
                        return;
                    }
                }
                if (thunderstorm())
                {
                    if (SpellManager.CanCast(Thunderstorm))
                    {
                        SpellManager.Cast(Thunderstorm);
                        LogMsg("Thunderstorm", 8);
                        LastSpellID = Thunderstorm;
                        return;
                    }
                }
                if (lavaburst())
                {
                    if (SpellManager.CanCast(LavaBurst))
                    {
                        SpellManager.Cast(LavaBurst);
                        LogMsg("Lava Burst", 4);
                        return;
                    }
                }
                if (lightningbolt())
                {
                    if (SpellManager.CanCast(LightningBolt))
                    {
                        SpellManager.Cast(LightningBolt);
                        LogMsg("Lightning Bolt", 6);
                        LastSpellID = LightningBolt;
                        return;
                    }
                }

                 
            }
        }
        #endregion rotation

        #region aoerotation
        public void AoeRotation()
        {

            if (chainlightningAscendance())
            {
                if (SpellManager.CanCast(ChainLightning))
                {
                    SpellManager.Cast(ChainLightning);
                    LogMsg("Chain Lightning", 3);
                    LastSpellID = ChainLightning;
                    return;
                }
            }
            if (magmatotem())
            {
                if (SpellManager.CanCast(MagmaTotem))
                {
                    SpellManager.Cast(MagmaTotem);
                    LogMsg("Magma Totem", 4);
                    LastSpellID = MagmaTotem;
                    MagmaTimer.Restart();
                    if (SearingTimer.IsRunning) { SearingTimer.Reset(); }
                    PutTotems = true;
                    return;
                }
            }
            if (searingTotem())
            {
                if (SpellManager.CanCast(SearingTotem))
                {
                    SpellManager.Cast(SearingTotem);
                    LogMsg("Searing Totem", 2);
                    LastSpellID = SearingTotem;
                    SearingTimer.Restart();
                    PutTotems = true;
                    return;
                }
            }
            if (healingStreamTotem())
            {
                if (SpellManager.CanCast(HealingStreamTotem))
                {
                    SpellManager.Cast(HealingStreamTotem);
                    LogMsg("Healing Stream Totem", 2);
                    LastSpellID = HealingStreamTotem;
                    return;
                }
            }
            if (thunderstorm())
            {
                if (SpellManager.CanCast(Thunderstorm))
                {
                    SpellManager.Cast(Thunderstorm);
                    LogMsg("Thunderstorm", 8);
                    LastSpellID = Thunderstorm;
                    return;
                }
            }
            if (earthquake())
            {
                if (SpellManager.CanCast(Earthquake))
                {
                    Lua.DoString("CastSpellByName('" + Earthquake + "');");
                    {
                        SpellManager.ClickRemoteLocation(StyxWoW.Me.CurrentTarget.Location);
                    }
                    LogMsg("Earthquake", 6);
                }
                return;
            }
            if (chainlightning())
            {
                if (SpellManager.CanCast(ChainLightning))
                {
                    SpellManager.Cast(ChainLightning);
                    LogMsg("Chain Lightning", 3);
                    LastSpellID = ChainLightning;
                    if (ShammySettings.myPrefs.earthquake != 0)
                    {
                        CastEarthQuake = true;
                    }
                    return;
                }
            }
        }
        #endregion aoerotation
    }   
}
